package com.ev.k3cloudBusiness.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ev.framework.annotation.EvApiByToken;
import com.ev.framework.utils.R;
import com.ev.k3cloud.entity.K3CloudAttachmentEntity;
import com.ev.k3cloudBusiness.service.K3AttachmentService;
import com.ev.k3cloudBusiness.service.K3StkTransferService;
import com.ev.k3cloudBusiness.vo.K3StkTransferInsertVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "/", tags = "金蝶K3Cloud附件管理")
@RestController
public class K3AttachmentController {

    @Autowired
    K3AttachmentService k3AttachmentService;

    @EvApiByToken(value = "/apis/attachment/insert", method = RequestMethod.POST, apiTitle = "新增附件")
    @ApiOperation("新增附件")
    public R save(@RequestBody @ApiParam("param") K3CloudAttachmentEntity param) {
        try {
            String result = k3AttachmentService.save(param);
            JSONObject resultObj = JSON.parseObject(result);
            if( resultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getBooleanValue("IsSuccess") ) {
                return R.ok();
            } else {
                return R.error(resultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors").toString());
            }
        }
        catch (Exception ex) {
            return R.error(ex.getMessage());
        }
    }

    @EvApiByToken(value = "/apis/attachment/batchInsert", method = RequestMethod.POST, apiTitle = "批量新增附件")
    @ApiOperation("批量新增附件")
    public R batchSave(@RequestBody @ApiParam("param") List<K3CloudAttachmentEntity> param) {
        try {
            String result = k3AttachmentService.batchSave(param);
            JSONObject resultObj = JSON.parseObject(result);
            if( resultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getBooleanValue("IsSuccess") ) {
                return R.ok();
            } else {
                return R.error(resultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors").toString());
            }
        }
        catch (Exception ex) {
            return R.error(ex.getMessage());
        }
    }
}
